package roborally.model;
import roborally.model.interfaces.*;

/**
 * A class representing RepairKit objects (inheriting from EnergyItem) with an energy content and a weight.
 * The energy content of a RepairKit can be used by a Robot to increase its maximum energylevel (getMaxEnergy())
 * @Invar 	A repairkit always has a valid pair of energy and maxEnergy levels.
 * 			|EnergyAmount.isValidEnergy(getEnergy(),getMaxEnergy());
 * @Invar	|isValidWeight(getWeight())
 * @extends EnergyItem
 * @Implements	Terminatable
 * @Implements	EnergyHolder	
 */
public class RepairKit extends EnergyItem implements Terminatable, EnergyHolder {
	
	//CONSTRUCTORS
	/**
	 * Constructor for a RepairKit.
	 * @pre		The pair of EnergyAmounts as initial energy and maximum energy should be valid.
	 *			|EnergyAmount.isValidEnergy(initialEnergy, maxEnergy)
	 * @param 	initialEnergy
	 * @param 	maxEnergy
	 * @param 	weight
	 * @effect |super(weight, initialEnergy)
	 */
	public RepairKit(EnergyAmount initialEnergy, int weight) {
		super(weight, initialEnergy, EMAX_MAX);
		assert( EnergyAmount.isValidEnergy(initialEnergy, getMaxEnergy()) );
	}
	
	/**
	 * Constructor without parameters for a RepairKit object.
	 * @effect	|this(EMAX_MAX, Item.WEIGHT_DEFAULT)
	 */
	public RepairKit(){
		this(EMAX_MAX, Item.WEIGHT_DEFAULT);
	}
	
	//Energy related
	/**
	 * An EnergyAmount object indicating the default maximum energylevel of a repairkit.
	 */
	public static final EnergyAmount EMAX_MAX = new EnergyAmount(Double.MAX_VALUE);
	/**
	 * An EnergyAmount expressing the increase in energy content a RepairKit can experience when it is hit by a robot's laser
	 */
	public static final EnergyAmount HIT_ENERGY = new EnergyAmount(500);
	
	//MUTATORS
	/**
	 * The effect a RepairKit experiences when it is hit by the laser of a Robot
	 * @effect If the energy content of the repairkit can be increased by HIT_ENERGY without exceeding getMaxEnergy(), it is done
	 * 		   | if(EnergyAmount.isValidEnergy(getEnergy().add(HIT_ENERGY), getMaxEnergy())) then
	 * 		   | setEnergy(getEnergy().add(HIT_ENERGY))
	 * @effect If the energy content of the repairkit cannot be increased by HIT_ENERGY without exceeding getMaxEnergy(), it is set at getMaxEnergy()
	 * 		   | if(!EnergyAmount.isValidEnergy(getEnergy().add(HIT_ENERGY), getMaxEnergy())) then
	 * 		   | setEnergy(getMaxEnergy())
	 */
	@Override
	public void hit() {
		if(EnergyAmount.isValidEnergy(getEnergy().add(HIT_ENERGY), getMaxEnergy())){
			setEnergy(getEnergy().add(HIT_ENERGY)); //The energycontent of the RepairKit can be increased by HIT_ENERGY
		}
		else {//The energycontent of the RepairKit cannot be increased by HIT_ENERGY because of overflow, we set it at getMaxEnergy()
			setEnergy(getMaxEnergy());
		}
	}
}

